home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / Sample Editors⁄Viewers / Cappuccino / Source / CappuccinoAction.cpp < prev    next >
Encoding:
Text File  |  1995-12-11  |  17.7 KB  |  594 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        CappuccinoAction.cpp
  3.     
  4.     Contents:    Classes for undo/redo support.
  5.     
  6.     Written by:    Troy Gaul
  7.     
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9. */
  10.  
  11. // -- Compiler/Preprocessor Switches --
  12.  
  13. #ifndef _COMPILERDEFS_
  14. #include "CompDefs.h"
  15. #endif
  16.  
  17. // -- OpenDoc Utilities --
  18.  
  19. #ifndef _EXCEPT_
  20. // Exceptions define several important macros (eg. CHECKENV)
  21. // which are used in the SOM method dispatch glue. If Except.h
  22. // is not included early enough, exceptions may not be thrown
  23. // correctly when returning from a SOM method with the "ev" parameter set.
  24. #include <Except.h>
  25. #endif
  26.  
  27. // -- Cappuccino Includes --
  28.  
  29. #ifndef _CAPPUCCINOACTION_
  30. #include "CappuccinoAction.h"
  31. #endif
  32.  
  33. #ifndef _CAPPUCCINO_
  34. #include "Cappuccino.h"
  35. #endif
  36.  
  37. #ifndef _CAPPUCCINOCONTENT_
  38. #include "CappuccinoContent.h"
  39. #endif
  40.  
  41. #ifndef _CAPPUCCINODEF_
  42. #include "CappuccinoDef.h"
  43. #endif
  44.  
  45. #ifndef _CAPPUCCINOGLOBALS_
  46. #include "CappuccinoGlobals.h"
  47. #endif
  48.  
  49. // -- OpenDoc Includes --
  50.  
  51. #ifndef _ODTYPES_
  52. #include <ODTypes.h>
  53. #endif
  54.  
  55. #ifndef SOM_ODClipboard_xh
  56. #include <Clipbd.xh>
  57. #endif
  58.  
  59. #ifndef SOM_ODDragItemIterator_xh
  60. #include <DgItmIt.xh>
  61. #endif
  62.  
  63. #ifndef SOM_ODDragAndDrop_xh
  64. #include <DragDrp.xh>
  65. #endif
  66.  
  67. #ifndef SOM_ODUndo_xh
  68. #include <Undo.xh>
  69. #endif
  70.  
  71. // -- OpenDoc Utilities --
  72.  
  73. #ifndef _BARRAY_
  74. #include <BArray.h>
  75. #endif
  76.  
  77. #ifndef _FOCUSLIB_
  78. #include <FocusLib.h>
  79. #endif
  80.  
  81. #ifndef _ODDEBUG_
  82. #include <ODDebug.h>
  83. #endif
  84.  
  85. #ifndef SOM_ODSession_xh
  86. #include <ODSessn.xh>
  87. #endif
  88.  
  89. #ifndef _ODUTILS_
  90. #include <ODUtils.h>
  91. #endif
  92.  
  93. #ifndef _TEMPOBJ_
  94. #include <TempObj.h>
  95. #endif
  96.  
  97. #ifndef _USERSRCM_
  98. #include <UseRsrcM.h>
  99. #endif
  100.  
  101. //------------------------------------------------------------------------------
  102. // SCappuccinoActionState
  103. //------------------------------------------------------------------------------
  104.  
  105. struct SCappuccinoActionState
  106. {
  107.     CAction*        fAction;
  108. };
  109.  
  110. //==============================================================================
  111. // CAction
  112. //==============================================================================
  113. #pragma mark • CAction •
  114.  
  115. //------------------------------------------------------------------------------
  116. // Method:        Constructor
  117. // Origin:        CAction
  118. //------------------------------------------------------------------------------
  119.  
  120. CAction::CAction( Cappuccino* part, ActionCode actionCode )
  121. {
  122.     fPart = part;
  123.     fSession = ODGetSession(somGetGlobalEnvironment(), fPart->GetODPart());
  124.     fActionCode = actionCode;
  125. }
  126.  
  127. //------------------------------------------------------------------------------
  128. // Method:        Destructor
  129. // Origin:        CAction
  130. //------------------------------------------------------------------------------
  131.  
  132. CAction::~CAction()
  133. {
  134. }
  135.     
  136. //------------------------------------------------------------------------------
  137. // Method:        HandleDo
  138. // Origin:        CAction
  139. //
  140. // Description:    This function call the Do method of this class.  Generally,
  141. //                you should override that method to provide the actual
  142. //                implementation of the change you want stored in the action.
  143. //------------------------------------------------------------------------------
  144.  
  145. void CAction::HandleDo( Environment* ev )
  146. {
  147.     this->Do(ev);
  148.     this->AddToActionHistory(ev);
  149. }
  150.  
  151. //------------------------------------------------------------------------------
  152. // Method:        HandleUndo
  153. // Origin:        CAction
  154. //
  155. // Description:    This function call the Undo method of this class.  Generally,
  156. //                you should override that method to provide the actual
  157. //                implementation of the change you want stored in the action.
  158. //------------------------------------------------------------------------------
  159.  
  160. void CAction::HandleUndo( Environment* ev )
  161. {
  162.     this->Undo(ev);
  163. }
  164.  
  165. //------------------------------------------------------------------------------
  166. // Method:        HandleRedo
  167. // Origin:        CAction
  168. //
  169. // Description:    This function call the Redo method of this class.  Generally,
  170. //                you should override that method to provide the actual
  171. //                implementation of the change you want stored in the action.
  172. //------------------------------------------------------------------------------
  173.  
  174. void CAction::HandleRedo( Environment* ev )
  175. {
  176.     this->Redo(ev);
  177. }
  178.  
  179. //------------------------------------------------------------------------------
  180. // Method:        GetUndoRedoName
  181. // Origin:        CAction
  182. //------------------------------------------------------------------------------
  183.  
  184. const ODBoolean kUndoName = kODTrue;
  185. const ODBoolean kRedoName = kODFalse;
  186.  
  187. ODName* CAction::GetUndoRedoName( Environment*        ev,
  188.                                   ODBoolean            isUndo )
  189. {
  190.     ODUShort kindOffset = (isUndo) ? kUndoOffset : kRedoOffset;
  191.     
  192.     // Start withe the prefix.
  193.     Str255 nameStr;
  194.     ODGetIndString(nameStr, kActionStringResID, 
  195.                    (fActionCode - 1) * kActionCodeMultiplier + kindOffset);
  196.     return CreateITextPString(gGlobals->fEditorsScript, gGlobals->fEditorsLanguage, 
  197.                               nameStr);
  198. }
  199.  
  200. //------------------------------------------------------------------------------
  201. // Method:        AddToActionHistory
  202. // Origin:        CAction
  203. //------------------------------------------------------------------------------
  204.  
  205. void CAction::AddToActionHistory( Environment* ev, ODActionType actionType )
  206. {
  207.     // Get the undo and redo menu item strings.
  208.     TempODName undoName = this->GetUndoRedoName(ev, kUndoName);
  209.     TempODName redoName = this->GetUndoRedoName(ev, kRedoName);
  210.  
  211.     // Stuff our action data into a ByteArray and add it to the action history.
  212.     ODUndo* undo = fSession->GetUndo(ev);
  213.     TempODActionData actionData = this->CreateActionData();
  214.     
  215.     undo->AddActionToHistory(ev, fPart->GetODPart(), actionData, 
  216.                              actionType, undoName, redoName);
  217. }
  218.  
  219. //------------------------------------------------------------------------------
  220. // Function:    CreateActionData
  221. // Origin:        CAction
  222. //
  223. // Description:    This method is called when this action must be encapsulated
  224. //                into an ODActionData byte array.
  225. //------------------------------------------------------------------------------
  226.  
  227. ODActionData* CAction::CreateActionData()
  228. {
  229.     SCappuccinoActionState state;
  230.     state.fAction = this;
  231.  
  232.     return CreateByteArray(&state, sizeof state);
  233. }
  234.  
  235. //------------------------------------------------------------------------------
  236. // Function:    GetActionStateAction                                [static]
  237. // Origin:        CAction
  238. //
  239. // Description:    This method is called when the action object referred to by
  240. //                an ODActionData byte array needs to be recovered so that it
  241. //                can be used.
  242. //------------------------------------------------------------------------------
  243.  
  244. CAction* CAction::GetActionStateAction(ODActionData* actionState)
  245. {
  246.     return ((SCappuccinoActionState*) actionState->_buffer)->fAction;
  247. }
  248.  
  249. //==============================================================================
  250. // CTextChangeAction
  251. //==============================================================================
  252. #pragma mark • CTextChangeAction •
  253.  
  254. //------------------------------------------------------------------------------
  255. // Method:        Constructor
  256. // Origin:        CTextChangeAction
  257. //------------------------------------------------------------------------------
  258.  
  259. CTextChangeAction::CTextChangeAction( Cappuccino*            part, 
  260.                                       ActionCode            actionCode,
  261.                                       CCappuccinoContent*    newContent)
  262.     : CAction(part, actionCode)
  263. {
  264.     fCurrentContent  = kODNULL;
  265.     fPreviousContent = kODNULL;
  266.     
  267.     if (newContent)
  268.         newContent->Acquire();
  269.     fCurrentContent = newContent;
  270. }
  271.  
  272. //------------------------------------------------------------------------------
  273. // Method:        Destructor
  274. // Origin:        CTextChangeAction
  275. //------------------------------------------------------------------------------
  276.  
  277. CTextChangeAction::~CTextChangeAction()
  278. {
  279.     if (fPreviousContent)
  280.         fPreviousContent->Release();
  281.     if (fCurrentContent)
  282.         fCurrentContent->Release();
  283. }
  284.  
  285. //------------------------------------------------------------------------------
  286. // Method:        Do
  287. // Origin:        CTextChangeAction
  288. //------------------------------------------------------------------------------
  289.  
  290. void CTextChangeAction::Do( Environment* ev )
  291. {
  292.     // Get the previous content at the time the action is performed.
  293.     fPreviousContent = fPart->GetContent();
  294.     if (fPreviousContent)
  295.         fPreviousContent->Acquire();
  296.     
  297.     fPart->SetContent(ev, fCurrentContent);
  298. }
  299.  
  300. //------------------------------------------------------------------------------
  301. // Method:        Undo
  302. // Origin:        CTextChangeAction
  303. //------------------------------------------------------------------------------
  304.  
  305. void CTextChangeAction::Undo( Environment* ev )
  306. {
  307.     fPart->SetContent(ev, fPreviousContent);
  308. }
  309.  
  310. //------------------------------------------------------------------------------
  311. // Method:        Redo
  312. // Origin:        CTextChangeAction
  313. //------------------------------------------------------------------------------
  314.  
  315. void CTextChangeAction::Redo( Environment* ev )
  316. {
  317.     fPart->SetContent(ev, fCurrentContent);
  318. }
  319.  
  320. //==============================================================================
  321. // CClipboardTextChangeAction
  322. //==============================================================================
  323. #pragma mark • CClipboardTextChangeAction •
  324.  
  325. //------------------------------------------------------------------------------
  326. // Method:        Constructor
  327. // Origin:        CClipboardTextChangeAction
  328. //------------------------------------------------------------------------------
  329.  
  330. CClipboardTextChangeAction::CClipboardTextChangeAction( Cappuccino*            part, 
  331.                                                         ActionCode            actionCode,
  332.                                                         ODCloneKind            cloneKind,
  333.                                                         CCappuccinoContent*    content)
  334.     : CTextChangeAction(part, actionCode, content)
  335. {
  336.     fCloneKind = cloneKind;
  337.     fUpdateID = kODUnknownUpdate;
  338. }
  339.  
  340. //------------------------------------------------------------------------------
  341. // Method:        Destructor
  342. // Origin:        CClipboardTextChangeAction
  343. //------------------------------------------------------------------------------
  344.  
  345. CClipboardTextChangeAction::~CClipboardTextChangeAction()
  346. {
  347. }
  348.  
  349. //------------------------------------------------------------------------------
  350. // Method:        HandleDo
  351. // Origin:        CClipboardTextChangeAction
  352. //------------------------------------------------------------------------------
  353.  
  354. void CClipboardTextChangeAction::HandleDo( Environment* ev )
  355. {
  356.     fUpdateID = fSession->GetClipboard(ev)->ActionDone(ev, fCloneKind);
  357.     
  358.     Inherited::HandleDo(ev);
  359. }
  360.  
  361. //------------------------------------------------------------------------------
  362. // Method:        HandleUndo
  363. // Origin:        CClipboardTextChangeAction
  364. //------------------------------------------------------------------------------
  365.  
  366. void CClipboardTextChangeAction::HandleUndo( Environment* ev )
  367. {
  368.     Inherited::HandleUndo(ev);
  369.     
  370.     // Let the clipboard know.
  371.     WASSERT(fUpdateID != kODUnknownUpdate);
  372.     fSession->GetClipboard(ev)->ActionUndone(ev, fUpdateID, fCloneKind);
  373. }
  374.  
  375. //------------------------------------------------------------------------------
  376. // Method:        HandleRedo
  377. // Origin:        CClipboardTextChangeAction
  378. //------------------------------------------------------------------------------
  379.  
  380. void CClipboardTextChangeAction::HandleRedo( Environment* ev )
  381. {
  382.     Inherited::HandleRedo(ev);
  383.  
  384.     // Let the clipboard know.
  385.     WASSERT(fUpdateID != kODUnknownUpdate);
  386.     fSession->GetClipboard(ev)->ActionRedone(ev, fUpdateID, fCloneKind);
  387. }
  388.  
  389. //==============================================================================
  390. // CCutAction
  391. //==============================================================================
  392. #pragma mark • CCutAction •
  393.  
  394. //------------------------------------------------------------------------------
  395. // Method:        Constructor
  396. // Origin:        CCutAction
  397. //------------------------------------------------------------------------------
  398.  
  399. CCutAction::CCutAction( Cappuccino* part )
  400.     : CClipboardTextChangeAction(part, kActionCut, kODCloneCut, kODNULL)
  401. {
  402.     Environment* ev = somGetGlobalEnvironment();
  403.  
  404.     fCurrentContent = new CCappuccinoContent(part);
  405.     fCurrentContent->InitCappuccinoContent(ev);
  406. }
  407.  
  408. //==============================================================================
  409. // CPasteAction
  410. //==============================================================================
  411. #pragma mark • CPasteAction •
  412.  
  413. //------------------------------------------------------------------------------
  414. // Method:        Constructor
  415. // Origin:        CPasteAction
  416. //------------------------------------------------------------------------------
  417.  
  418. CPasteAction::CPasteAction( Cappuccino* part, CCappuccinoContent* content )
  419.     : CClipboardTextChangeAction(part, kActionPaste, kODClonePaste, content)
  420. {
  421. }
  422.  
  423. //==============================================================================
  424. // CClearAction
  425. //==============================================================================
  426. #pragma mark • CClearAction •
  427.  
  428. //------------------------------------------------------------------------------
  429. // Method:        Constructor
  430. // Origin:        CClearAction
  431. //------------------------------------------------------------------------------
  432.  
  433. CClearAction::CClearAction( Cappuccino* part )
  434.     : CTextChangeAction(part, kActionClear, kODNULL)
  435. {
  436.     Environment* ev = somGetGlobalEnvironment();
  437.  
  438.     fCurrentContent = new CCappuccinoContent(part);
  439.     fCurrentContent->InitCappuccinoContent(ev);
  440. }
  441.  
  442. //==============================================================================
  443. // CDropAction
  444. //==============================================================================
  445. #pragma mark • CDropAction •
  446.  
  447. //------------------------------------------------------------------------------
  448. // Method:        Constructor
  449. // Origin:        CDropAction
  450. //------------------------------------------------------------------------------
  451.  
  452. CDropAction::CDropAction( Cappuccino* part, CCappuccinoContent* content )
  453.     : CTextChangeAction(part, kActionDrop, content)
  454. {
  455. }
  456.  
  457. //==============================================================================
  458. // CDragBeginAction
  459. //==============================================================================
  460. #pragma mark • CDragBeginAction •
  461.  
  462. //------------------------------------------------------------------------------
  463. // Method:        Constructor
  464. // Origin:        CDragBeginAction
  465. //------------------------------------------------------------------------------
  466.  
  467. CDragBeginAction::CDragBeginAction( Cappuccino* part )
  468.     : CAction(part, kActionDrag)
  469. {
  470. }
  471.  
  472. //------------------------------------------------------------------------------
  473. // Method:        HandleDo
  474. // Origin:        CDragBeginAction
  475. //------------------------------------------------------------------------------
  476.  
  477. void CDragBeginAction::HandleDo( Environment* ev )
  478. {
  479.     this->AddToActionHistory(ev, kODBeginAction);
  480.     // Don't call Inherited as we're replacing its functionality.
  481. }
  482.  
  483. //==============================================================================
  484. // CDragEndAction
  485. //==============================================================================
  486. #pragma mark • CDragEndAction •
  487.  
  488. //------------------------------------------------------------------------------
  489. // Method:        Constructor
  490. // Origin:        CDragEndAction
  491. //------------------------------------------------------------------------------
  492.  
  493. CDragEndAction::CDragEndAction( Cappuccino* part )
  494.     : CAction(part, kActionDrag)
  495. {
  496. }
  497.  
  498. //------------------------------------------------------------------------------
  499. // Method:        HandleDo
  500. // Origin:        CDragEndAction
  501. //------------------------------------------------------------------------------
  502.  
  503. void CDragEndAction::HandleDo( Environment* ev )
  504. {
  505.     this->AddToActionHistory(ev, kODEndAction);
  506.     // Don't call Inherited as we're replacing its functionality.
  507. }
  508.  
  509. //==============================================================================
  510. // CSetTextAction
  511. //==============================================================================
  512. #pragma mark • CSetTextAction •
  513.  
  514. //------------------------------------------------------------------------------
  515. // Method:        Constructor
  516. // Origin:        CSetTextAction
  517. //------------------------------------------------------------------------------
  518.  
  519. CSetTextAction::CSetTextAction( Cappuccino* part, CCappuccinoContent* content )
  520.     : CTextChangeAction(part, kActionSetText, content)
  521. {
  522. }
  523.  
  524. //==============================================================================
  525. // CSettingsChangeAction
  526. //==============================================================================
  527. #pragma mark • CSettingsChangeAction •
  528.  
  529. //------------------------------------------------------------------------------
  530. // Method:        Constructor
  531. // Origin:        CSettingsChangeAction
  532. //------------------------------------------------------------------------------
  533.  
  534. CSettingsChangeAction::CSettingsChangeAction( Cappuccino*        part, 
  535.                                               CSettings*        newSettings)
  536.     : CAction(part, kActionChangeSettings)
  537. {
  538.     fCurrentSettings  = kODNULL;
  539.     fPreviousSettings = kODNULL;
  540.     
  541.     if (newSettings)
  542.         newSettings->Acquire();
  543.     fCurrentSettings = newSettings;
  544. }
  545.  
  546. //------------------------------------------------------------------------------
  547. // Method:        Destructor
  548. // Origin:        CSettingsChangeAction
  549. //------------------------------------------------------------------------------
  550.  
  551. CSettingsChangeAction::~CSettingsChangeAction()
  552. {
  553.     if (fPreviousSettings)
  554.         fPreviousSettings->Release();
  555.     if (fCurrentSettings)
  556.         fCurrentSettings->Release();
  557. }
  558.  
  559. //------------------------------------------------------------------------------
  560. // Method:        Do
  561. // Origin:        CSettingsChangeAction
  562. //------------------------------------------------------------------------------
  563.  
  564. void CSettingsChangeAction::Do( Environment* ev )
  565. {
  566.     // Get the previous content at the time the action is performed.
  567.     fPreviousSettings = fPart->GetSettings();
  568.     if (fPreviousSettings)
  569.         fPreviousSettings->Acquire();
  570.     
  571.     fPart->SetSettings(ev, fCurrentSettings);
  572. }
  573.  
  574. //------------------------------------------------------------------------------
  575. // Method:        Undo
  576. // Origin:        CSettingsChangeAction
  577. //------------------------------------------------------------------------------
  578.  
  579. void CSettingsChangeAction::Undo( Environment* ev )
  580. {
  581.     fPart->SetSettings(ev, fPreviousSettings);
  582. }
  583.  
  584. //------------------------------------------------------------------------------
  585. // Method:        Redo
  586. // Origin:        CSettingsChangeAction
  587. //------------------------------------------------------------------------------
  588.  
  589. void CSettingsChangeAction::Redo( Environment* ev )
  590. {
  591.     fPart->SetSettings(ev, fCurrentSettings);
  592. }
  593.  
  594.